﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dukou.Domain;
using NHibernate;
using NHibernate.Criterion;

namespace Dukou.Dao.Implement
{
    [Serializable]
    public class EnumerationDao : NHibernateDao<Enumeration, Int64>, IEnumerationDao
    {
        public IList<Enumeration> SelectByCategory(long categoryId)
        {
            return this.HibernateTemplate.ExecuteFind<Enumeration>(delegate(ISession session) 
            {
                return session.CreateCriteria(typeof(Enumeration), "e")
                    .CreateAlias("e.Category", "c")
                    .Add(Restrictions.Eq("c.Id", categoryId))
                    .List<Enumeration>();
            });
        }

        public IList<Enumeration> SelectByCategory(string category)
        {
            return this.HibernateTemplate.ExecuteFind<Enumeration>(delegate(ISession session)
            {
                return session.CreateCriteria(typeof(Enumeration), "e")
                    .CreateAlias("e.Category", "c")
                    .Add(Restrictions.Eq("c.Name", category))
                    .List<Enumeration>();
            });
        }

        public IList<Enumeration> SelectByCategory(EnumerationCategory category)
        {
            return this.SelectByCategory(category.Id);
        }
    }
}
